package com.idc.common.config;

import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import com.alibaba.fastjson.JSON;
import com.idc.common.util.AES;
import com.idc.common.util.RadixUtil;

import lombok.Data;

@Configuration
@ConfigurationProperties(prefix = "app")
public class AppConfig {

	@Data
	public static class AesConfigration {
		// 解密根
		private String radixs;
		// 密钥编码
		private String code;
		// 密钥
		private String appAesSecretKey;
	}
	
	@Data
	public static class AesConfigrationVo {
		// 密钥编码
		private String code;
	}

	private AesConfigration config;

	public AesConfigration getConfig() {
		return config;
	}

	public void setConfig(AesConfigration config) {
		System.out.println("update config==========================");
		if (StringUtils.isEmpty(config.getAppAesSecretKey()) || config.getAppAesSecretKey().length() != 16) {
			throw new RuntimeException("error appAesSecretKey");
		}
		this.config = config;
		setAesConfigrationVo(JSON.parseObject(JSON.toJSONString(config), AesConfigrationVo.class));
		this.aesConfigrationVo.setCode(RadixUtil.decodeToBigDecimal(config.getRadixs(), config.getAppAesSecretKey()).toString());
		AES.refreshAppSecretKey(config.getAppAesSecretKey());
	}

	private AesConfigrationVo aesConfigrationVo;

	public AesConfigrationVo getAppConfigrationVo() {
		return aesConfigrationVo;
	}

	public void setAesConfigrationVo(AesConfigrationVo aesConfigrationVo) {
		this.aesConfigrationVo = aesConfigrationVo;
	}


}
